import { SubSeqOfResult } from "../axdr/ProxyGetResponseList";
import { FrameData } from "../bean/FrameData";
import { DataUtils } from "../DataUtils";
import { resultCode } from "../tools/DataTool";
import { ProxyGetResponseRecord } from "../axdr/ProxyGetResponseRecord";
import { ProxyTransCommandResponse } from "../axdr/ProxyTransCommandResponse";
import { ProxySetResponseList } from "../axdr/ProxySetResponseList";
import { ProxySetThenGetResponseList } from "../axdr/ProxySetThenGetResponseList";
import { ProxyActionResponseList } from "../axdr/ProxyActionResponseList";
import { ProxyActionThenGetResponseList } from "../axdr/ProxyActionThenGetResponseList";
import { parseData } from "./ParseTools";


export function parseProxyGetResponseList(subSeqOfResult: SubSeqOfResult): String {
    let frameData = new FrameData();
    let seq_proxy_list = {
        seq_proxy_list: {

        }
    }
    let seq_proxy_data = {
        seq_of_data: {

        },
    }
    let proxy_list = new Array<any>();
    for (let i = 0; i < subSeqOfResult.size(); i++) {
        let proxy_data = new Array<any>();
        for (let j = 0; j < subSeqOfResult.get(i).result.size(); j++) {
            switch (subSeqOfResult.get(i).result.get(j).result.getChoiceIndex()) {
                case 0:
                    let dar = {
                        dar: {},
                    };
                    subSeqOfResult.get(i).result.get(j).result.dar.encodeAndSave(1000);
                    dar.dar = DataUtils.bytesToHexString(subSeqOfResult.get(i).result.get(j).result.dar.dataCode, 0, subSeqOfResult.get(i).result.get(j).result.dar.dataCode.length);
                    proxy_data.push(dar);
                    break;
                case 1:
                    let value = parseData(subSeqOfResult.get(i).result.get(j).result.data);
                    proxy_data.push(value);
                    break;
            }
        }
        seq_proxy_data.seq_of_data = proxy_data;
        proxy_list.push(seq_proxy_data);
    }
    seq_proxy_list.seq_proxy_list = proxy_list;
    frameData.data = seq_proxy_list;
    frameData.code = resultCode.success;
    return JSON.stringify(frameData);
}

export function parseProxyGetRecord(proxyGetResponseRecord: ProxyGetResponseRecord): String {
    let frameData = new FrameData();
    switch (proxyGetResponseRecord.result.respData.getChoiceIndex()) {
        case 0:
            let dar = {
                dar: {},
            };
            proxyGetResponseRecord.result.respData.dar.encodeAndSave(1000);
            dar.dar = DataUtils.bytesToHexString(proxyGetResponseRecord.result.respData.dar.dataCode, 0, proxyGetResponseRecord.result.respData.dar.dataCode.length);
            frameData.data = dar;
            break;
        case 1:
            let seq_of_a_record = {
                seq_of_a_record: {},
            };
            let a_record = {
                a_record: {},
            }
            let seq_a_record_list = new Array<any>();
            for (let i = 0; i < proxyGetResponseRecord.result.respData.records.size(); i++) {
                let seq_data = {
                    seq_of_data: {}
                }
                let seq_data_list = new Array<String>();
                for (let j = 0; j < proxyGetResponseRecord.result.respData.records.get(i).data.size(); j++) {
                    let value = parseData(proxyGetResponseRecord.result.respData.records.get(i).data.get(j));
                    seq_data_list.push(value);
                }
                a_record.a_record = seq_data_list
                seq_data.seq_of_data = seq_data_list;
                seq_a_record_list.push(a_record);
            }
            seq_of_a_record.seq_of_a_record = seq_a_record_list;
            frameData.data = seq_of_a_record;
            break;
    }
    frameData.code = resultCode.success;
    return JSON.stringify(frameData);
}

export function parseProxyTransCommand(proxyTransCommandResponse: ProxyTransCommandResponse): String {
    let frameData = new FrameData();
    let transResult = {
        transResult: {}
    }
    switch (proxyTransCommandResponse.result.getChoiceIndex()) {
        case 0:
            let dar = {
                dar: {},
            };
            proxyTransCommandResponse.result.dar.encodeAndSave(1000);
            dar.dar = DataUtils.bytesToHexString(proxyTransCommandResponse.result.dar.dataCode, 0, proxyTransCommandResponse.result.dar.dataCode.length);
            transResult.transResult = dar;
            break;
        case 1:
            let oct_string = {
                oct_string: {}
            }
            oct_string.oct_string = proxyTransCommandResponse.result.retResult.toString();
            transResult.transResult = oct_string;
            break;
    }
    frameData.data = transResult;
    frameData.code = resultCode.success;
    return JSON.stringify(frameData);
}

export function parseProxySetResponseNormalList(proxySetNormalList: ProxySetResponseList): String {
    let frameData = new FrameData();
    let proxy_set_response_list = {
        seq_of_dar: {}
    }
    let darArray = new Array<any>();
    for (let i = 0; i < proxySetNormalList.result.size(); i++) {
        let proxy_set = {
            seq_dar: {}
        }
        let darList = new Array<any>();
        let proxySet = proxySetNormalList.result.get(i);
        for (let j = 0; j < proxySet.result.size(); j++) {
            let dar = {
                dar: {}
            }
            let setOfResult = proxySet.result.get(j);
            setOfResult.dar.encodeAndSave(1000);
            dar.dar = DataUtils.bytesToHexString(setOfResult.dar.dataCode, 0, setOfResult.dar.dataCode.length);
            darList.push(dar);
        }
        proxy_set.seq_dar = darList;
        darArray.push(proxy_set);
    }
    frameData.code = resultCode.success;
    proxy_set_response_list.seq_of_dar = darArray;
    frameData.data = proxy_set_response_list;
    return JSON.stringify(frameData);
}

export function parseProxySetThenGetResponseList(proxySetThenGet: ProxySetThenGetResponseList): String {
    let frameData = new FrameData();
    let proxy_set_then_get_list = {
        seq_of_result: {}
    }
    let resultArray = new Array<any>();
    for (let i = 0; i < proxySetThenGet.result.size(); i++) {
        let proxySetThenGetResponse = proxySetThenGet.result.get(i);
        let result = new Array<any>();
        let proxy_set_then_get = {
            seq_result: {}
        }
        for (let j = 0; j < proxySetThenGetResponse.result.size(); j++) {
            let set_dar = {
                dar: {},
                get_result: {}
            }
            let setThenGet = proxySetThenGetResponse.result.get(j);
            setThenGet.dar.encodeAndSave(1000);
            set_dar.dar = DataUtils.bytesToHexString(setThenGet.dar.dataCode, 0, setThenGet.dar.dataCode.length)
            let get_result = setThenGet.getResult;
            switch (get_result.choice) {
                case 0:
                    let dar = {
                        dar: {}
                    }
                    get_result.dar.encodeAndSave(1000);
                    dar.dar = DataUtils.bytesToHexString(get_result.dar.dataCode, 0, get_result.dar.dataCode.length);
                    set_dar.get_result = dar;
                    break;
                case 1:
                    set_dar.get_result = parseData(get_result.data);
                    break;
            }
            result.push(set_dar);
        }
        proxy_set_then_get.seq_result = result;
        resultArray.push(proxy_set_then_get);
    }
    proxy_set_then_get_list.seq_of_result = resultArray;
    frameData.code = resultCode.success;
    frameData.data = proxy_set_then_get_list;
    return JSON.stringify(frameData);
}

export function parseProxyActionResponseList(proxyActionList: ProxyActionResponseList): String {
    let frameData = new FrameData();
    let proxy_action_response_list = {
        seq_result: {}
    }
    let resultArray = new Array<any>();
    for (let i = 0; i < proxyActionList.result.size(); i++) {
        let proxy_action_response = {
            seq_action_result: {}
        }
        let resultList = new Array<any>();
        let proxyAction = proxyActionList.result.get(i);
        for (let j = 0; j < proxyAction.result.size(); j++) {
            let action_result = {
                dar: {},
                data_optio: {
                    data: {}
                }
            }
            let actionList = proxyAction.result.get(j);
            actionList.dar.encodeAndSave(1000);
            action_result.dar = DataUtils.bytesToHexString(actionList.dar.dataCode, 0, actionList.dar.dataCode.length);
            if (actionList.retData.isUsed) {
                action_result.data_optio.data = parseData(actionList.retData.value);
            } else {
                action_result.data_optio.data = {}
            }
            resultList.push(actionList);
        }
        proxy_action_response.seq_action_result = resultList;
        resultArray.push(proxy_action_response);
    }
    proxy_action_response_list.seq_result = resultArray;
    frameData.code = resultCode.success;
    frameData.data = proxy_action_response_list;
    return JSON.stringify(frameData);
}

export function parseProxyActionThenGetResponseList(proxyActionThenGetList: ProxyActionThenGetResponseList): String {
    let frameData = new FrameData();
    let proxy_action_then_get_list = {
        seq_of_result: {}
    }
    let resultArray = new Array<any>();
    for (let i = 0; i < proxyActionThenGetList.result.size(); i++) {
        let proxyActionThenGet = proxyActionThenGetList.result.get(i);
        let proxy_action_then_get = {
            seq_of_action_then_get_result: {}
        }
        let resultList = new Array<any>();
        for (let j = 0; j < proxyActionThenGet.result.size(); j++) {
            let action_then_get_result = {
                dar: {},
                data_option: {
                    data: {}
                },
                get_result: {}
            }
            let actionThenGet = proxyActionThenGet.result.get(j);
            actionThenGet.dar.encodeAndSave(1000);
            action_then_get_result.dar = DataUtils.bytesToHexString(actionThenGet.dar.dataCode, 0, actionThenGet.dar.dataCode.length);
            if (actionThenGet.retData.isUsed) {
                action_then_get_result.data_option.data = parseData(actionThenGet.retData.value);
            } else {
                action_then_get_result.data_option.data = {};
            }
            let get_result = actionThenGet.result;
            switch (get_result.choice) {
                case 0:
                    let dar = {
                        dar: {}
                    }
                    get_result.dar.encodeAndSave(1000);
                    dar.dar = DataUtils.bytesToHexString(get_result.dar.dataCode, 0, get_result.dar.dataCode.length);
                    action_then_get_result.get_result = dar;
                    break;
                case 1:
                    action_then_get_result.get_result = parseData(get_result.data);
                    break;
            }
            resultList.push(action_then_get_result);
        }
        proxy_action_then_get.seq_of_action_then_get_result = resultList;
        resultArray.push(proxy_action_then_get);
    }
    frameData.code = resultCode.success
    proxy_action_then_get_list.seq_of_result = resultArray;
    frameData.data = proxy_action_then_get_list;
    return JSON.stringify(frameData);
}
